#include "comm.hpp"

#include <string>

#include <cstring>
#include <signal.h>

int main()
{
    int msgid = Message_Queue::get();
    std::cout << "获取消息队列成功, msgid: " << msgid << std::endl;

    msg_buf snd;
    int tmp = msgid;
    Message_Queue::recieve(msgid, &snd, 1);
    pid_t rid = *(pid_t*)snd.mtext;

    std::cout << "等待接收: " << std::endl;
    while (true)
    {
        Message_Queue::recieve(msgid, &snd, 1);
        snd.mtext[strlen(snd.mtext) - 1] = '\0';
        std::cout << "client sent: " << snd.mtext << std::endl;
        if (strcmp(snd.mtext, "exit") == 0)
        {
            std::cout << "client exit" << std::endl;
            kill(rid, SIGKILL);
            break;
        }
    }

    Message_Queue::free(msgid);
    std::cout << "销毁消息队列成功" << std::endl;
    return 0;
}